package com.sebklebes.radar.httpclient;

import android.location.Location;
import android.util.Log;
import com.sebklebes.radar.converter.RadarEntity;
import com.sebklebes.radar.converter.ResponseConverter;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class HttpClient {

	private static final String TAG = HttpClient.class.getName();

	private static final String URL = "http://android-radar.appspot.com/";
	private static final String RADAR_PAGE = "home.html";

	private static final String SUCCESS = "success";
	private static final String ERROR = "error";

	private static final String ADD_RADAR = "add_radar";
	private static final String GET_RADARS = "get_radars";

	private static List<RadarEntity> cachedRadars = new ArrayList<RadarEntity>();

	public static List<RadarEntity> sendLocation(Location location) {
		// Create a new HttpClient and Post Header
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(URL + RADAR_PAGE);

		try {
			// Add your data
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
			nameValuePairs.add(new BasicNameValuePair("lat", String
					.valueOf(location.getLatitude())));
			nameValuePairs.add(new BasicNameValuePair("long", String
					.valueOf(location.getLongitude())));
			nameValuePairs.add(new BasicNameValuePair("action", GET_RADARS));
			nameValuePairs.add(new BasicNameValuePair("range", "500"));
			nameValuePairs.add(new BasicNameValuePair("maxNrResults", "20"));

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			ResponseHandler<String> res = new BasicResponseHandler();
			// Execute HTTP Post Request
			String response = httpclient.execute(httppost, res);
			List<RadarEntity> radarEntities = ResponseConverter.convertToRadarList(response);

			List<RadarEntity> newRadars = new ArrayList<RadarEntity>();

			for (RadarEntity radar : radarEntities) {
				if (!cachedRadars.contains(radar)) {
					newRadars.add(radar);
				}
			}

			cachedRadars = radarEntities;

			return newRadars;
		} catch (ClientProtocolException e) {
			Log.e(TAG, e.getMessage());
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
		}
		return null;
	}

	public static String addRadar(Location location) {
		// Create a new HttpClient and Post Header
		DefaultHttpClient httpclient = new DefaultHttpClient();
		HttpPost httppost = new HttpPost(URL + RADAR_PAGE);

		try {
			// Add your data
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
			nameValuePairs.add(new BasicNameValuePair("lat", String
					.valueOf(location.getLatitude())));
			nameValuePairs.add(new BasicNameValuePair("long", String
					.valueOf(location.getLongitude())));
			nameValuePairs.add(new BasicNameValuePair("action", ADD_RADAR));

			httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

			// Execute HTTP Post Request
			ResponseHandler<String> res = new BasicResponseHandler();
			// Execute HTTP Post Request
			String response = httpclient.execute(httppost, res);

			RadarEntity radarEntity = ResponseConverter.convertToRadar(response);

			if (!cachedRadars.contains(radarEntity)) {
				cachedRadars.add(radarEntity);
			}

			return SUCCESS;
		} catch (ClientProtocolException e) {
			Log.e(TAG, e.getMessage());
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
		}
		return ERROR;
	}

	public static void clearCache() {
		cachedRadars.clear();
	}
}
